/**
 * Created by zgt   2017/05/10
 */
import logTmp from './form/log.html';
import logCtrl from './form/log.ctrl';
import alertTmp from './form/alert.html';
import alertCtrl from './form/alert.ctrl';
class Service {
    constructor(LesseeSrv, $state, list, ctgNotification, $stateParams, $uiModal, $scope) {
        "ngInject";
        this._serviceSrv = LesseeSrv;
        this._state = $state;
        this._notification = ctgNotification;
        this.dbCode = $stateParams.kDbCode;
        this._uiModal = $uiModal;
        this._scope = $scope;
        this.page = {
            currentPage: 1,
            totalItems: 0
        };
        this.filter = {
            name: null,
            code: null
        };
        if (list) {
            this.table = list.list;
            this.page.totalItems = list.total;
        } else this.table = [];
    }

    getList() {
        this.table = null;
        this._serviceSrv.getRCList({ page: this.page.currentPage, code: this.dbCode }, this.filter).then(data => {
            this.table = data.list;
            this.page.totalItems = data.total;
        }).catch(() => {
            this.table = [];
            this.page.totalItems = 0;
            this._notification.error('获取服务列表数据有误.');
        });
    }

    search() {
        this.page.currentPage = 1;
        this.getList();
    }
    lessee_edit(item) {
        // this._scope.$parent.lessee_item = item;
        this._state.go('create.basemsg', { kDbCode: this.dbCode,lesId:item.id });
    }
    openLogForm(title, yaml) {
        this._uiModal.open({
            animation: true,
            templateUrl: logTmp,
            controller: logCtrl,
            controllerAs: 'vm',
            size: 'lg',
            resolve: {
                yaml: function() {
                    return yaml;
                },
                title: function() {
                    return title;
                }
            }
        });
    }
    delete(elem) {
        var moreMsg=undefined;
        if (elem.usedRcs > 0) {
            moreMsg="注意：此租户下有"+elem.usedRcs+"个应用。";
        }
        this._uiModal.open({
            templateUrl: alertTmp,
            controller: alertCtrl,
            controllerAs: 'vm',
            windowClass: 'alert-dialog alert-dialog-ctg',
            resolve: {
                alert: function() {
                    return {
                        appCode: elem.namespaceCode,
                        action: '删除',
                        moreMsg:moreMsg
                    }
                }
            }
        }).result.then(() => {
            
                elem.deleting = true;
                this._serviceSrv.deleteLessee(elem).then(() => {
                    this._notification.success('删除成功.');
                    this.getList();
                }).catch(() => {
                    this._notification.success('删除失败.');
                }).finally(() => {
                    
                    elem.deleting = false;
                })
           

        });



    }

    getCpus(elem){

    elem.usedMemory=parseInt(elem.usedMemory);
       elem.usedCpu=(parseInt(elem.usedCpu)/(elem.cpuQuota*1000)*100).toFixed(2);
    }
}

export default Service;